//
//  NSObject+THToast.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSObject+THToast.h"
@implementation NSObject (THToast)
+ (void)load
{
    [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
}

- (void)showToast:(NSString*)toast inView:(UIView *)view
{
    if (![toast isKindOfClass:NSString.class]) {
        return;
    }
    if(!view){
        return;
    }
    
    if (toast.length <= 0) {
        return;
    }
    [self hiddenAllHud];

    NSAssert([NSThread isMainThread], @"MBProgressHUD needs to be accessed on the main thread.");
    UIWindow *window   =  view;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = toast;
    hud.label.numberOfLines = 3;
    hud.margin     = 15.f;
    hud.label.numberOfLines = 0;
    hud.userInteractionEnabled = NO;
    hud.minSize = CGSizeMake(100, kSizeRateTo(50));
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color =  [[UIColor blackColor] colorWithAlphaComponent:0.8];
    hud.bezelView.layer.cornerRadius = kSizeRateTo(25);
    hud.bezelView.layer.masksToBounds = YES;
    hud.label.textColor=[UIColor whiteColor];
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.5];
}

- (void)showToast:(NSString*)toast
{
    [self showToast:toast inView:[UIApplication sharedApplication].keyWindow];
}

- (void)showTipsView:(NSString *)tips
            afterDelay:(NSTimeInterval)delay
         completeBlock:(MBProgressHUDCompletionBlock)completeBlock
{
    if (![tips isKindOfClass:NSString.class]) {
        return;
    }
    
    if (tips.length <= 0) {
        return;
    }
    NSAssert([NSThread isMainThread], @"MBProgressHUD needs to be accessed on the main thread.");
    [self hiddenAllHud];
    UIWindow *window   =  [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.label.text = tips;
    hud.label.numberOfLines = 0;
    hud.margin     = 15.f;
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    hud.completionBlock = completeBlock;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color =  [[UIColor blackColor] colorWithAlphaComponent:0.8];
    hud.minSize = CGSizeMake(100, kSizeRateTo(50));
    hud.bezelView.layer.cornerRadius = kSizeRateTo(25);
    hud.bezelView.layer.masksToBounds = YES;
    hud.label.textColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:delay];
}

- (void)showLoading
{
    [self hiddenAllHud];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow?:[THCurrentVCWindowsManager shareManager].currentWindow animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    hud.mode = MBProgressHUDModeIndeterminate;
}

- (void)showLoading:(NSString *)tips
{
    [self hiddenAllHud];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow?:[THCurrentVCWindowsManager shareManager].currentWindow animated:YES];
    hud.label.text = tips;
    hud.label.numberOfLines = 0;
    hud.label.textColor = [UIColor whiteColor];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    hud.minSize = CGSizeMake(100, kSizeRateTo(50));
//    hud.bezelView.layer.cornerRadius = kSizeRateTo(25);
//    hud.bezelView.layer.masksToBounds = YES;
    hud.mode = MBProgressHUDModeIndeterminate;

}

- (void)hiddenLoading
{
    [self hiddenAllHud];
}
- (BOOL)hiddenAllHud
{
    return [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow?:[THCurrentVCWindowsManager shareManager].currentWindow animated:YES];
}
- (void)showLoadingInView:(UIView*)inView{
    if(!inView){
        return;
    }
    [self hidenHUDInView:inView];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:inView animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    hud.mode = MBProgressHUDModeIndeterminate;
}
- (void)hidenHUDInView:(UIView*)inView{
    if(!inView){
        return;
    }
    [MBProgressHUD hideHUDForView:inView animated:YES];
}

@end
